package com.mypog;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


@SuppressWarnings("serial")
@WebServlet("/SetShips")
public class SetShips extends HttpServlet {	
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {				
				
		// Get user session.
		HttpSession session = request.getSession();
		String playerName = (String) session.getAttribute("username");
		
		String values = request.getParameter("items");
		String[] valueArray = values.split(",");
		
		// Get player object.
		AllUsers allusers = new AllUsers();
		
		//Get user.
		User player = allusers.getUserObject(playerName, "user");
		
		// Fill ship array with data.
		if (player != null){
			player.setShipsPlayer(new int[100]);
			for (int i = 0; i < valueArray.length; i++) {
				if (valueArray[i].equals("undefined")){
					player.setShip(i, 0);
				} else {
					player.setShip(i, 1);
				}										
			}
			player.setReady(true);
		}
	}

}
